package com.tarena.practice.manage.util;

import java.io.File;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
public class UploadFileUtils {
	@Value("${uploadfile.basepath}")
	private String basePath;
	
	@Value("${uploadfile.imgpath}")
	private String imgPath;
	
	@Value("${uploadfile.access-file}")
	private String accessFile;

	public String getBasePath() {
		return basePath;
	}

	public void setBasePath(String basePath) {
		this.basePath = basePath;
	}

	public String getImgPath() {
		return imgPath;
	}

	public void setImgPath(String imgPath) {
		this.imgPath = imgPath;
	}

	public String getAccessFile() {
		return accessFile;
	}

	public void setAccessFile(String accessFile) {
		this.accessFile = accessFile;
	}

	/**
	 * 刪除文件
	 * @param srcFilePath
	 * @throws Exception
	 */
	public void deleteFile(String srcFilePath) throws Exception {
		if(srcFilePath == null || srcFilePath == "") {
			return;
		} 
		// 多个文件路径进行逗号，分割
		String[] paths = srcFilePath.split(",");
		// 依次遍历各个文件路径
		for (String path : paths) {
			// 对原有路径进行重新拼接操作，以得到文件在服务器上存储的实际路径
			String imgUrl = path.substring(("http://" + accessFile).length());
			String sourceUrl = basePath + imgUrl;
			// 获取该文件，虚拟
			File file = new File(sourceUrl);
			// 判断文件是否存在，
			if(file.exists()) {
				// 若存在，则进行删除操作 
				file.delete();
			}
		}
	}
	
	@Override
	public String toString() {
		return "FileUploadUtils [basePath=" + basePath + ", imgPath=" + imgPath + ", accessFile=" + accessFile + "]";
	}
	
	
}
